﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using LitJson;
using SufeiUtil;

namespace Live
{
    public class PandaTv : BasePlatform
    {
        private readonly PandaTvRoom room = new PandaTvRoom();

        public override bool Init(string url)
        {
            //https://www.panda.tv/1586361
            if (string.IsNullOrEmpty(url))
            {
                return false;
            }

            string[] urlSplit = url.Split(new char[] {'/'});

            if (urlSplit.Length != 4)
            {
                return false;
            }
            //房间id
            string roomId = GetRoomId(url);
            if (string.IsNullOrEmpty(roomId))
            {
                return false;
            }
            //房间信息
            string html = ReqRoomInfo(roomId);

            JsonData data = JsonMapper.ToObject(html);
            int errno = int.Parse(data["errno"].ToString());
            if (errno == 0)
            {
                room.UserId = data["data"]["hostinfo"]["rid"].ToString();
                room.UserName = data["data"]["hostinfo"]["name"].ToString();
                room.UserAvatar = data["data"]["hostinfo"]["avatar"].ToString();

                room.ID = data["data"]["roominfo"]["id"].ToString();
                room.Name = data["data"]["roominfo"]["name"].ToString();
                room.Bulletin = data["data"]["roominfo"]["bulletin"].ToString();
                room.Details = data["data"]["roominfo"]["details"].ToString();

                room.Plflag = data["data"]["videoinfo"]["plflag"].ToString();
                room.VideoStatus = data["data"]["videoinfo"]["status"].ToString();
                room.RoomKey = data["data"]["videoinfo"]["room_key"].ToString();

                string pList = data["data"]["videoinfo"]["plflag_list"].ToString();
                pList = pList.Replace("\\", "");
                JsonData pData = JsonMapper.ToObject(pList);
                room.Sign = pData["auth"]["sign"].ToString();
                room.Time = pData["auth"]["time"].ToString();
                room.Rid = pData["auth"]["rid"].ToString();
            }
            else
            {
                return false;
            }

            return true;
        }

        protected override string ReqRoomInfo(string roomId)
        {
            string url = String.Format("http://www.panda.tv/api_room_v2?roomid={0}", roomId);

            HttpItem item = new HttpItem();
            item.Method = "GET";
            item.URL = url;
            item.Host = "www.panda.tv";
            item.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            item.UserAgent =
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";

            return mHelper.GetHtml(item).Html;
        }

        public override bool IsLive()
        {
            return room.VideoStatus.EndsWith("2");
        }

        public override Room GetRoomInfo()
        {
            return room;
        }

        public override string GetLivePlayUrl()
        {
            return string.Format("http://pl{0}.live.panda.tv/live_panda/{1}.flv?sign={2}&ts={3}&rid={4}",
                room.Plflag.Split(new char[] {'_'})[1], room.RoomKey,
                room.Sign, room.Time, room.Rid);
        }
    }
}